package com.device.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.device.Constant.BaseConstant;
import com.device.Entity.Dto.SessionWebUserDto;
import com.device.Entity.User;
import com.device.Exception.BaseException;
import com.device.Mapper.UserMapper;
import com.device.Service.UserService;
import com.device.common.PageResult;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    public SessionWebUserDto login(String username, String password, Integer roles) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        lambdaQueryWrapper.eq(User::getPassword, password);
        lambdaQueryWrapper.eq(User::getRole, roles);
        User user = userMapper.selectOne(lambdaQueryWrapper);
        if (user == null) {
            throw new BaseException("用户不存在");
        }
        if (user.getDelflag().equals(BaseConstant.ZERO)) {
            throw new BaseException("用户被禁用");
        }
        SessionWebUserDto sessionWebUserDto = new SessionWebUserDto();
        BeanUtils.copyProperties(user, sessionWebUserDto);

        return sessionWebUserDto;

    }

    public void register(String username, String password, Integer roles) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        lambdaQueryWrapper.eq(User::getPassword, password);
        lambdaQueryWrapper.eq(User::getRole, roles);
        User user = userMapper.selectOne(lambdaQueryWrapper);
        if (user != null) {
            throw new BaseException("该用户已存在");
        }
        User user1 = new User();
        user1.setUsername(username);
        user1.setDelflag(BaseConstant.ONE);
        user1.setPassword(password);
        user1.setRole(roles);
//        user1.setRole(BaseConstant.TWO);
        userMapper.insert(user1);


    }

    public void adduser(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        lambdaQueryWrapper.eq(User::getRole, user.getRole());
        User users = userMapper.selectOne(lambdaQueryWrapper);
        if (users != null) {
            throw new BaseException("该用户已存在");
        }
        users.setDelflag(BaseConstant.ONE);
        userMapper.insert(user);

    }

    public PageResult getpage(Integer pageNo, Integer pageSize, String keyword) {
        IPage<User> page = new Page<>(pageNo, pageSize);
        IPage<User> list = userMapper.page(page, keyword, BaseConstant.ONE);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;
    }

    public PageResult getuserpage(Integer pageNo, Integer pageSize, String keyword) {
        IPage<User> page = new Page<>(pageNo, pageSize);
        IPage<User> list = userMapper.page(page, keyword, BaseConstant.TWO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(list.getTotal());
        pageResult.setRecords(list.getRecords());
        return pageResult;

    }

    public void edituser(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        lambdaQueryWrapper.eq(User::getRole, user.getRole());
        lambdaQueryWrapper.notIn(User::getId, user.getId());
        User users = userMapper.selectOne(lambdaQueryWrapper);
        if (users != null) {
            throw new BaseException("该用户已存在");
        }
        userMapper.updateById(user);
    }

    public void del(Integer id) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getId, id);
        User users = userMapper.selectOne(lambdaQueryWrapper);
        if (users.getDelflag().equals(BaseConstant.ZERO)) {
            throw new BaseException("该用户不存在");
        }
        users.setDelflag(BaseConstant.ZERO);
        userMapper.updateById(users);


    }

    public List<Map<String, Object>> getlabuser() {
        List<Map<String, Object>> lists = new ArrayList<>();
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getRole, BaseConstant.ONE);
        lambdaQueryWrapper.eq(User::getDelflag,BaseConstant.ONE);
        List<User> list = userMapper.selectList(lambdaQueryWrapper);
        for (User user : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", user.getId());
            map.put("name", user.getName());
            lists.add(map);
        }
        return lists;

    }
}
